<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :badge-data="badgeData" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
      >
        <template #action="{ row }">
          <el-button-group>
            <!-- <el-button type="success" size="small" @click="openRepay(row)">
              标记已收款
            </el-button> -->
            <el-button type="info" size="small" @click="openDetail(row)">
              相关明细
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>

    <DetailTrade v-model="showTradeDetail" :data="current" @done="reload" />
    <RebateDetail v-model="showRebateDetail" :data="current" @done="reload" />
    <PayInsuranceDetail
      v-model="showPayInsuranceDetail"
      :data="current"
      @done="reload"
    />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Search from './components/search.vue';
  import DetailTrade from './components/detail-trade.vue';
  import RebateDetail from './components/rebate-from-insurance.vue';
  import PayInsuranceDetail from './components/pay-to-insurance.vue';

  import { pagePayRecord } from '@/api/company';

  const badgeData = ref({
    judge: 0
  });
  /** 表格实例 */
  const tableRef = ref(null);
  const defaultWhere = ref({
    status: 0
  });

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        prop: 'id',
        label: 'ID',
        width: 85,
        align: 'center'
      },
      {
        width: 120,
        prop: 'cate',
        label: '分类'
      },
      {
        width: 80,
        prop: 'type',
        label: '类型'
      },
      {
        width: 120,
        prop: 'amount',
        label: '金额'
      },
      {
        prop: 'relation_name',
        label: '交易对象'
      },
      {
        width: 100,
        prop: 'create_user',
        label: '创建人'
      },
      {
        width: 160,
        prop: 'payTime',
        label: '付款时间'
      }
    );
    if (currentStatus.value == 200) {
      cols.push(
        {
          width: 100,
          label: '复核人',
          prop: 'judge_user'
        },
        {
          width: 160,
          label: '复核时间',
          prop: 'judgeTime'
        }
      );
    }
    cols.push({
      width: 100,
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      fixed: 'right'
    });
    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pagePayRecord({
      ...where,
      ...orders,
      page,
      limit
    });
    badgeData.value = result.badgeData;

    return result;
  };

  const current = ref({});
  /** 详情 */
  const showTradeDetail = ref(false);
  const showRebateDetail = ref(false);
  const showPayInsuranceDetail = ref(false);
  const openDetail = (row) => {
    current.value = row;
    if (row.cate == '出单收款') showTradeDetail.value = true;
    if (row.cate == '返点收入') showRebateDetail.value = true;
    if (row.cate == '保险结账') showPayInsuranceDetail.value = true;
  };
  const currentStatus = ref(0);
  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
    currentStatus.value = where ? where.status : 0;
  };
</script>
